<!--
 * @Version: 1.0
 * @Date: 2021-04-07 18:42:19
 * @LastEditTime: 2021-05-13 19:30:09
 * @Description:
-->
<template>
  <div>
    <el-dialog
      :center="true"
      :title="title"
      :visible.sync="formVisibleDown"
      width="500px"
      :before-close="() => {$emit('update:formVisibleDown', false)}"
      @open="openDialog"
    >
      <div>
        <el-form ref="ruleForm" :model="ruleForm" inline :rules="rules">
          <el-form-item label="修改金额" prop="amount">
            <el-input v-model="ruleForm.amount" placeholder="请输入修改金额" />
          </el-form-item>
        </el-form>
        <div style="text-align: center; margin-top:50px">
          <el-button type="primary" :loading="isbtn" @click="submitForm('ruleForm')">保存</el-button>
          <el-button @click="handleCancelDown">取消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    formVisibleDown: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '录入费用修改'
    },
    id: {
      type: String,
      default: ''
    },
    amount: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isbtn: false,
      ruleForm: {
        amount: ''
      },
      rules: {
        amount: [
          { required: true, message: '请输入修改金额', trigger: 'blur' },
          { pattern: /\d+(\.[0-9]{1,2}){0,1}$/, message: '只能输入数字,最多四位小数', trigger: 'change' }
        ]
      }
    }
  },

  methods: {
    openDialog() {
      Object.keys(this.ruleForm).forEach(k => {
        this.ruleForm[k] = ''
      })
      if (this.amount && this.amount > 0) {
        this.ruleForm.amount = this.amount
      }
    },
    // 关闭
    handleCancelDown() {
      this.$emit('update:formVisibleDown', false)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.ruleForm
          this.$http.put(`/afterSaleOrders/setModifyAmount/${this.id}`, data).then((res) => {
            if (res.code === 10000) {
              this.$message.success('修改金额成功')
              this.$emit('handleSearch')
              this.handleCancelDown()
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

 <style lang="scss" scoped>

 </style>
